package com.baijia.dao.impl;

import com.baijia.dao.UserLoginLogDao;
import com.baijia.dao.po.UserLoginLog;
import com.baijia.util.JdbcUtil;

import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by dudu on 2016/11/3.
 */
@Repository
public class UserLoginLogDaoImpl extends CommonDaoImpl<UserLoginLog, Long> implements UserLoginLogDao {

    public UserLoginLogDaoImpl() {
        super(UserLoginLog.class);
    }

    private static String INSERT_SQL = "insert into cdb.user_login_log(user_id, login_ip, login_time, compaign_id, user_role, stat_area_id, stat_ip, stat_client, stat_channel," +
        " stat_device, version, stat_mac, stat_imei, auth_type) values(?,?,now(),?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    public void save(final UserLoginLog log) {
        jdbcTemplate.update(INSERT_SQL, new PreparedStatementSetter(){

            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                int index = 0;
                JdbcUtil.setLong(ps, ++index, log.getUserId());
                JdbcUtil.setString(ps, ++index, log.getLoginIp());
                JdbcUtil.setString(ps, ++index, log.getCompaignId());
                JdbcUtil.setInt(ps, ++index, log.getUserRole());
                JdbcUtil.setInt(ps, ++index, log.getStatAreaId());
                JdbcUtil.setString(ps, ++index, log.getStatIp());
                JdbcUtil.setInt(ps, ++index, log.getStatClient());
                JdbcUtil.setString(ps, ++index, log.getStatChannel());
                JdbcUtil.setString(ps, ++index, log.getStatDevice());
                JdbcUtil.setString(ps, ++index, log.getVersion());
                JdbcUtil.setString(ps, ++index, log.getStatMac());
                JdbcUtil.setString(ps, ++index, log.getStatImei());
                JdbcUtil.setInt(ps, ++index, log.getAuthType());
            }
        });
    }

    @Override
    protected String selectSql() {
        return null;
    }
}
